﻿using Microsoft.Multipoint.Sdk;
using System;
using System.Windows;
using System.Windows.Input;
using UTable.Core.Diagnostics;
using UTable.Core.Objects.Controls.Tabletop;
using UTable.Core.Windows;
using System.Collections.ObjectModel;

namespace UTable.Core.Input
{
    public class MultiMouseProvider : IInputProvider
    {
        #region Private Methods

        private UApplication context;
        
        #endregion

        #region Properties

        /// <summary>
        /// The UApplication this multi-mice belongs to.
        /// </summary>
        public UApplication Context
        {
            get { return context; }
            private set
            {
                context = value;
                Mouse.OverrideCursor = Cursors.None;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initialization work will be done in Initialize(UApplication context)
        /// </summary>
        public MultiMouseProvider() { }

        #endregion

        #region IInputProvider Methods

        /// <summary>
        /// Triggered when meeting a new mouse event
        /// TODO realize InputTriggered
        /// </summary>
        public event InputEventHandler InputTriggered;

        /// <summary>
        /// Actual Initialization method
        /// </summary>
        /// <param name="context">The root application</param>
        public void Initialize(UTable.Core.Windows.UApplication context)
        {
            this.Context = context;
            MultipointSdk.Instance.Register(context.MainWindow);
            RegisterMultipointMouseEvents(context.Root);
        }

        #endregion

        #region Private Methods

        private void RegisterMultipointMouseEvents(UTabletop tabletop)
        {
            tabletop.MultipointMouseDownEvent += OnMultipointMouseDown;
            tabletop.MultipointMouseUpEvent += OnMultipointMouseUp;
            tabletop.MultipointMouseMoveEvent += OnMultipointMouseMove;
        }

        #region Mouse Events Handler

        private void OnMultipointMouseDown(object sender, RoutedEventArgs e)
        {
            MultipointMouseEventArgs me = e as MultipointMouseEventArgs;
            FingerEventArgs fingerEvent = new FingerEventArgs();
            fingerEvent.Timestamp = Time.GetTimestamp();
            fingerEvent.EventType = FinggerEventType.FINGER_DOWN_EVENT;
            Point p = new Point(me.LastX, me.LastY);
            p = this.Context.Root.PointFromScreen(p);
            fingerEvent.GlobalPosition = fingerEvent.Position = p;
            fingerEvent.UserID = 999;
            fingerEvent.DeviceName = this.GetType().ToString();
            fingerEvent.Orientation = 0;
            fingerEvent.Handled = fingerEvent.Captured = false;

            //TODO Raise and deal with FingerEvent
        }

        private void OnMultipointMouseMove(object sender, RoutedEventArgs e)
        {
            //TODO
        }

        private void OnMultipointMouseUp(object sender, RoutedEventArgs e)
        {
            //TODO
        }


        #endregion

        #endregion

        #region Public Methods

        #endregion
    }
}
